<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-upload
          :action="''"
          :http-request="handleUpload"
          :show-file-list="false"
          multiple
        >
          <el-button type="primary" plain icon="Plus">上传文件</el-button>
        </el-upload>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="documentList">
      <el-table-column label="文档ID" align="center" prop="document_id" width="300" />
      <el-table-column label="文件名" align="center" prop="name" />
      <el-table-column label="文件大小" align="center" prop="size">
        <template #default="scope">
          <span>{{ formatFileSize(scope.row.size) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字符数" align="center" prop="char_count" />
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="danger"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Documents">
import { getDocuments, uploadFiles, delDocument } from "@/api/app-store/dataset";
import { useRoute } from 'vue-router';

const route = useRoute();
const { proxy } = getCurrentInstance();

const loading = ref(true);
const total = ref(0);
const documentList = ref([]);
const datasetId = ref(route.query.datasetId);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10
});

/** 查询文件列表 */
function getList() {
  loading.value = true;
  getDocuments(datasetId.value, queryParams.value).then(response => {
    documentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 文件上传请求 */
function handleUpload(options) {
  const { file } = options;
  uploadFiles([file], datasetId.value).then(response => {
    proxy.$modal.msgSuccess("上传成功");
    getList();
  }).catch(error => {
    console.error("上传失败:", error);
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除该文件？').then(function() {
    return delDocument(datasetId.value, row.document_id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();

/** 格式化文件大小 */
function formatFileSize(size) {
  if (!size) return '0 B';
  const units = ['B', 'KB', 'MB', 'GB'];
  let index = 0;
  while (size >= 1024 && index < units.length - 1) {
    size /= 1024;
    index++;
  }
  return `${size.toFixed(2)} ${units[index]}`;
}
</script>